//
//  VisitorLoginView.swift
//  SinaWeibo
//
//  Created by 石林 on 15/11/11.
//  Copyright © 2015年 石林. All rights reserved.
//

import UIKit

protocol VisitorLoginViewDelegate: NSObjectProtocol {
    
    func visitorWillLogin()
    func visitorWillRegistor()
}

class VisitorLoginView: UIView {
    
    weak var visitorDelegate:VisitorLoginViewDelegate?
    
    @objc func loginDidClick() {
        visitorDelegate?.visitorWillLogin()
    }
    
    @objc func registerDidClick() {
        visitorDelegate?.visitorWillRegistor()
    }

    func setUIInfo(imageName: String?, title: String) {
        iconView.hidden = false
        if imageName != nil {
            iconView.image = UIImage(named: imageName!)
            circleView.hidden = true
        } else {
            startAnimation()
        }
        tipLabel.text = title
    }
    
    private func startAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.repeatCount = MAXFLOAT
        anim.toValue = 2 * M_PI
        anim.duration = 20
        anim.removedOnCompletion = false
        circleView.layer.addAnimation(anim, forKey: nil)
        
    }
    
    init() {
        super.init(frame: CGRectZero)
        updateUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func updateUI() {
        
        addSubview(circleView)
        addSubview(backView)
        addSubview(iconView)
        addSubview(tipLabel)
        
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        //圆圈的约束
        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
        
        //小房子的约束
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: circleView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        
        //提示文案约束
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 226))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 34))
        
        //登录按钮约束
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Left, relatedBy: .Equal, toItem: tipLabel, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        
        //注册按钮约束
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Right, relatedBy: .Equal, toItem: tipLabel, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        //蒙板约束
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backView]-0-|", options: [], metrics: nil, views: ["backView": backView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backView]-(-35)-[loginBtn]", options: [], metrics: nil, views: ["backView": backView, "loginBtn": loginBtn]))
        
        backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        loginBtn.addTarget(self, action: "loginDidClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "registerDidClick", forControlEvents: .TouchUpInside)
        
    }
    
    //MARK:懒加载控件
    lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    lazy var backView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    lazy var tipLabel: UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜", size: 14, color: UIColor.lightGrayColor())
//    {
//        let l = UILabel()
//        l.text = "关注一些人，回这里看看有什么惊喜"
//        l.textAlignment = NSTextAlignment.Center
//        l.font = UIFont.systemFontOfSize(14)
//        l.textColor = UIColor.lightGrayColor()
//        l.numberOfLines = 0
//        return l
//    }()
    
    lazy var loginBtn: UIButton = UIButton(title: "登录", backImage: "common_button_white_disable", color: UIColor.lightGrayColor())
//    {
//        let btn = UIButton()
//        btn.setTitle("登录", forState: .Normal)
//        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
//        btn.setTitleColor(UIColor.lightGrayColor(), forState: .Normal)
//        return btn
//    }()
    
    lazy var registerBtn: UIButton = UIButton(title: "注册", backImage: "common_button_white_disable", color: UIColor.orangeColor())
//    {
//        let btn = UIButton()
//        btn.setTitle("注册", forState: .Normal)
//        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
//        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
//        return btn
//    }()
}
